package com.example.administrator.zkdemo.dao;

import android.database.Cursor;

import com.example.administrator.zkdemo.db.orm.manager.DataManager;
import com.example.administrator.zkdemo.model.ZKMeetBean;

import java.util.ArrayList;
import java.util.List;

public class ZKSignInDao {
    private DataManager mDb;

    public ZKSignInDao(DataManager paramDataManager) {
        this.mDb = paramDataManager;
    }

    public void deleteAttLog(int paramInt) {
        try {
            String sql = "   DELETE FROM        ATT_LOG    WHERE        id    IN(        SELECT            id        FROM        ATT_LOG        ORDER BY            id ASC        LIMIT " +
                    paramInt +
                    "    ) ";
            this.mDb.queryBySql(sql).close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getActualCount(int paramInt) {
        int result = 0;
        try {
            String sql = " SELECT      count(DISTINCT User_PIN) AS count  FROM      ATT_LOG al  WHERE      al.User_PIN   IN(      SELECT          pm.User_Pin      FROM          MEET_INFO mi      LEFT JOIN          PERS_MEET pm      ON          mi.Code = pm.Code      WHERE          mi.Code = " +
                    paramInt +
                    "  )  AND      al.Verify_Time  BETWEEN(      SELECT          MetStarSignTm      FROM          MEET_INFO      WHERE          Code = " +
                    paramInt +
                    "  )  AND(      SELECT          LatRetTm      FROM          MEET_INFO      WHERE          Code = " +
                    paramInt +
                    "  ) ";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToFirst()) {
                result = cursor.getInt(cursor.getColumnIndex("count"));
            }
            cursor.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return result;
    }

    public int getAttLogCount() {
        int result = 0;
        try {
            Cursor localCursor = this.mDb.queryBySql("   SELECT        count(*) as count    FROM        ATT_LOG ");
            if (localCursor.moveToFirst()) {
                result = localCursor.getInt(localCursor.getColumnIndex("count"));
            }
            localCursor.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return result;
    }

    public String getLastMeetTime(String paramString, int paramInt) {
        String result = "";
        try {
            String sql = "\tSELECT\t\tVerify_Time\tFROM\t\tMEET_LOG\tWHERE\t\tUser_PIN = '" +
                    paramString +
                    "'\tAND\t\tCode = " +
                    paramInt +
                    "\tORDER BY\t\tid DESC\tLIMIT \t\t1";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex("Verify_Time")).replace("T", " ");
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getLastVerifyTime(String paramString) {
        String result = "";
        try {
            String sql = "\tSELECT\t\tVerify_Time\tFROM\t\tATT_LOG\tWHERE\t\tUser_PIN = '" +
                    paramString +
                    "'\tORDER BY\t\tid DESC\tLIMIT \t\t1";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex("Verify_Time")).replace("T", " ");
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean getState(String paramString1, String paramString2) {
        String userPin = "";
        try {
            String sql = " SELECT      User_Pin  FROM      PERS_MEET  WHERE      Code = '" +
                    paramString1 +
                    "'  AND      User_Pin = '" +
                    paramString2 +
                    "' ";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToNext()) {
                userPin = cursor.getString(cursor.getColumnIndex("User_Pin"));
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (userPin != null) && (!userPin.equals(""));
    }

    public int getUserActualCount(String paramString1, String paramString2) {
        int result = 0;
        try {
            String sql = " SELECT      count(DISTINCT User_PIN) AS count  FROM      ATT_LOG  WHERE      Verify_Time   BETWEEN      '" +
                    paramString1 +
                    "'  AND      '" +
                    paramString2 +
                    "' ";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToFirst()) {
                result = cursor.getInt(cursor.getColumnIndex("count"));
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public int getUserCount() {
        int result = 0;
        try {
            Cursor localCursor = this.mDb.queryBySql(" SELECT      count(1) AS count FROM      USER_INFO ");
            if (localCursor.moveToFirst()) {
                result = localCursor.getInt(localCursor.getColumnIndex("count"));
            }
            localCursor.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return result;
    }

    public boolean meetHavePers(int paramInt, String paramString) {
        String result = "";
        try {
            String sql = "   SELECT    \tID    FROM    \tPERS_MEET    WHERE    \tCode = " +
                    paramInt +
                    "    AND    \tUser_Pin = '" +
                    paramString +
                    "' ";
            Cursor cursor = this.mDb.queryBySql(sql);
            if (cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex("ID"));
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (result != null) && (!result.equals(""));
    }

    /**
     * 查询用户是否是管理员
     *
     * @return
     */
    public int query14User() {
        int result = 0;
        try {
            Cursor localCursor = this.mDb.queryBySql("\tSELECT\t\tcount(1) AS count\tFROM\t\tUSER_INFO \tWHERE\t\tPrivilege = 14");
            if (localCursor.moveToFirst()) {
                result = localCursor.getInt(localCursor.getColumnIndex("count"));
            }
            localCursor.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return result;
    }

    public List<ZKMeetBean> queryMeetList() {
        ArrayList<ZKMeetBean> localArrayList = new ArrayList<>();
        try {
            Cursor localCursor = this.mDb.queryBySql("   SELECT        mi.Code,        mi.MetName,        mi.MetStrTm,        mi.MetEndTm,        (            SELECT                count(*)            FROM                PERS_MEET            WHERE                Code = mi.Code        ) AS count    FROM        MEET_INFO mi ");
            if (localCursor.moveToFirst()) {
                do {
                    ZKMeetBean localZKMeetBean = new ZKMeetBean();
                    localZKMeetBean.setCode(localCursor.getString(localCursor.getColumnIndex("Code")));
                    localZKMeetBean.setMetName(localCursor.getString(localCursor.getColumnIndex("MetName")));
                    localZKMeetBean.setMetStrTm(localCursor.getString(localCursor.getColumnIndex("MetStrTm")));
                    localZKMeetBean.setMetEndTm(localCursor.getString(localCursor.getColumnIndex("MetEndTm")));
                    localZKMeetBean.setCount(localCursor.getString(localCursor.getColumnIndex("count")));
                    localArrayList.add(localZKMeetBean);
                }
                while (localCursor.moveToNext());
            }
            localCursor.close();
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return localArrayList;
    }
}
